package com.tttare.haoyigou.sellers.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tttare.haoyigou.pojo.TbBrand;
import com.tttare.haoyigou.pojo.TbBrandExample;
import com.tttare.haoyigou.pojo.TbBrandExample.Criteria;
import com.tttare.haoyigou.sellers.service.BrandService;
import com.tttare.haoyigou.mapper.TbBrandMapper;

import entity.PageResult;

@Service
public class BrandServiceImpl implements BrandService{

	@Autowired
	private TbBrandMapper tbBrandMapper;

	@Override
	public List<TbBrand> findAll() {
		List<TbBrand> list = tbBrandMapper.selectByExample(null);
		return list;
	}

	@Override
	public  PageResult<TbBrand> findPage(int page, int size) {
		PageHelper.startPage(page, size);//pageHelper就是这么分页的,很神奇
		Page<TbBrand> pageContent = (Page)tbBrandMapper.selectByExample(null);
		//封装数据在PageResult中
		PageResult<TbBrand> pageResult = new PageResult<TbBrand>();
		pageResult.setTotal(pageContent.getTotal());
		pageResult.setRows(pageContent.getResult());
		return pageResult;
	}

	@Override
	public void add(TbBrand tbBrand) {
		tbBrandMapper.insert(tbBrand);
		
	}
	
	@Override
	public void update(TbBrand tbBrand) {
		tbBrandMapper.updateByPrimaryKey(tbBrand);
	}

	@Override
	public PageResult<TbBrand> findPage(TbBrand tbbrand, int page, int size) {
		//先分页
		PageHelper.startPage(page, size);
		//如果TbBrand不为null就条件查询(Example是专门用来条件查询的类)
		TbBrandExample example = new TbBrandExample();
		Criteria criteria = example.createCriteria();
		if(tbbrand!=null){
			if(tbbrand.getFirstChar()!=null&&StringUtils.isNotBlank(tbbrand.getFirstChar())){
				criteria.andFirstCharEqualTo(tbbrand.getFirstChar());
			}
			if(tbbrand.getName()!=null&&StringUtils.isNotBlank(tbbrand.getName())){
				criteria.andNameLike("%"+tbbrand.getName()+"%");
			}
		}
		Page<TbBrand> pageContent = (Page<TbBrand>)tbBrandMapper.selectByExample(example);
		//封装数据在PageResult中
		PageResult<TbBrand> pageResult = new PageResult<TbBrand>();
		pageResult.setTotal(pageContent.getTotal());
		pageResult.setRows(pageContent.getResult());
		return pageResult;
	}

	@Override
	public TbBrand findById(Long id) {
		TbBrand brand = tbBrandMapper.selectByPrimaryKey(id);
		return brand;
	}

	@Override
	public List<Map> selectOptionList() {
		List<Map> list = tbBrandMapper.selectOptionList();
		return list;
	}

	
}
